//
//  SWOriginalViewModel.swift
//  SinaWeibo
//
//  Created by wszyxc on 16/7/24.
//  Copyright © 2016年 wszyxc. All rights reserved.
//

import UIKit

class SWOriginalViewModel: NSObject {

    var homeModel:SWHomeModel?
    
    var memberImage:UIImage?
    
    var sourceText:NSMutableAttributedString?
    
    var timeText:String?
    
    
    //转发数
    var repostsCount:String?
    
    //评论
    var commentsCount:String?
    
    //赞
    var attitudesCount:String?
    
    //重载构造函数
    init(homeModel:SWHomeModel) {
        
        super.init()
        
        self.homeModel = homeModel
        
        memberImage = setMemberImage(homeModel.user?.verified ?? 0)

        sourceText = getSoureceText(homeModel.source)
        
        timeText = getTimeText()
        
        repostsCount = getComponentsStr(homeModel.reposts_count, state: "转发")
        
        commentsCount = getComponentsStr(homeModel.comments_count, state: "评论")
        
        attitudesCount = getComponentsStr(homeModel.attitudes_count, state: "赞")
     
    }
    
    //设置转发评论赞的文字显示
    private func getComponentsStr(count:Int,state:String) ->String{
    
        
        
        if count>0{
            
            if count<10000 {
                return "\(count)"
            }else{
                
                if count%10000 == 0{
                    
                    return "\(count/10000)万"
                }else{
                    
                    return  "\((Float(count/1000))/10)万"
                }
            }
            
            
        }else{
            
            return state
        }
    
    }
    
    //设置微博来源的内容
    private func getSoureceText(source:String?) ->NSMutableAttributedString{
   
        guard let sc = source where sc.containsString("\">") else{
            
            return getAttributeStr("星星的你")
        }
        
        let startRange = sc.rangeOfString("\">")!
        
        let endRange = sc.rangeOfString("</")!
        
        let str = sc.substringWithRange(startRange.endIndex..<endRange.startIndex)
        
        
   
        return getAttributeStr(str)
    }
    
    private func getAttributeStr(str:String) ->NSMutableAttributedString {
        
        let name = "来自 " + str
        
        let range = (name as NSString).rangeOfString("来自")
        
        
      //  print(range)
        
        //创建 富文本(可变)
        let attStr = NSMutableAttributedString(string: name)
        
     //   attStr.addAttribute(NSForegroundColorAttributeName, value: UIColor.grayColor(), range: range)
        
        attStr.addAttributes([NSForegroundColorAttributeName:UIColor.grayColor()], range: range)
        
        return attStr
        
    }
    
    
    private func setMemberImage(verified:Int) ->UIImage?{
        /// 认证类型 -1：没有认证，1，认证用户，2,3,5: 企业认证，220: 达人
        
        let img :UIImage?
        
        switch verified{
            
        case 1:img = UIImage(named: "avatar_vip")
            
        case 2,3,5:img = UIImage(named: "avatar_enterprise_vip")
            
        case 220:img = UIImage(named: "avatar_grassroot")
            
        default: img = UIImage(named: "avatar_vgirl")
            
            
        }
        
        return img
    }
    
    private func getTimeText() ->String {
        
        
        return NSDate.sinaDate((homeModel?.created_at)!)!.sinaDescripitionStr
    }
    
    
}
